/**
 * Copyright 2009 The University of North Carolina at Chapel Hill
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package irodsfedora.storage.lowlevel;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

import fedora.common.Constants;
import fedora.server.Context;
import fedora.server.ReadOnlyContext;
import fedora.server.Server;
import fedora.server.errors.InitializationException;
import fedora.server.errors.ObjectNotFoundException;
import fedora.server.errors.servletExceptionExtensions.InternalError500Exception;

/**
 * Provides a REST interface for accessing CDR Reports
 *
 * @author Greg Jansen
 */
public class ReportServlet extends HttpServlet implements Constants {

    private static final long serialVersionUID = 1L;

    /** Logger for this class. */
    private static final Logger LOG = Logger.getLogger(ReportServlet.class);

    /** Instance of the Fedora Commons Server */
    private static Server fcserver = null;

    public static final String ACTION_LABEL = "CDR Report";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	try {
	    Context context = ReadOnlyContext.getContext(Constants.HTTP_REQUEST.REST.uri, request);
	    PremisReport bean = new PremisReport(fcserver, context);
	    String pid = request.getParameter("pid");

	    // Note: Fedora APIM security check performed by container
	    // String sessionToken = request.getParameter("sessionToken");

	    // returns 404 for unrecognized PIDs
	    Document report = null;
	    try {
		report = bean.getXMLReport(pid);
	    } catch(ObjectNotFoundException e) {
		throw new fedora.server.errors.servletExceptionExtensions.NotFound404Exception("Object Not Found", e, request, ACTION_LABEL, "", new String[0]);
	    }

	    response.setContentType("text/xml; charset=UTF-8");

	    PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
	    new XMLOutputter().output(report, out);
	    out.flush();
	    out.close();
	} catch (Throwable th) {
	    throw new InternalError500Exception("", th, request, ACTION_LABEL, "", new String[0]);
	}
    }

    /** Unsupported HTTP Method */
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	// unimplemented
	throw new ServletException("Error: HTTP POST method is not supported, please use GET");
    }

    /** Gets the Fedora Server instance. */
    @Override
    public void init() throws ServletException {
	try {
	    fcserver = Server.getInstance(new File(Constants.FEDORA_HOME), false);
	} catch (InitializationException ie) {
	    throw new ServletException("Error getting Fedora Server instance: " + ie.getMessage());
	}
    }
}
